<?php

// @formatter:off
/**
 * @file image.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: image.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/content/image.php $
 */
// @formatter:on

class ActionControllerContentImage extends WCActionController {
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;
			global $wcImage;

			if(!$wcProfile->allowedTo(WC_PERM_IMAGE_VIEW)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You're not allowed to open images";
			}
			if($check == WC_HTTPERROR_OK && (!$wcImage || !$wcImage->ok())) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_BAD_REQUEST;
				$errno = $check;
				$error = "Invalid image";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;

		//
		// Loading global pointers.
		global $wcImage;
		global $wcProfile;
		global $wcCollection;
		global $wcGroup;

		$wcImage->expand();

		$this->_manager->PATH = "{$wcImage->directory()->name}/{$wcImage->name}";

		$this->_manager->mainimg = turlencode($wcImage->publicUri());
		//
		// Setting gallery info
		// @{
		if($wcProfile->allowedTo(WC_PERM_COMIC_RENAME)) {
			$this->_manager->info_name_editable = true;
		}
		if($wcProfile->allowedTo(WC_PERM_COMIC_SET_TAG)) {
			$this->_manager->info_tags_editable = true;
		}
		if($wcProfile->allowedTo(WC_PERM_COMIC_SET_COLLECTION)) {
			$this->_manager->info_coll_editable = true;
		}
		$this->_manager->info_public_name = basename($wcImage->publicUri());
		$this->_manager->info_directory_name = $wcImage->directory()->name();
		if($wcCollection) {
			$this->_manager->info_collections = $wcCollection->collectionPath();
		}

		WCViewInfoFactory::LoadInfo($wcImage, true);
		WCViewInfoFactory::LoadInterfacesInfo($wcImage, true);
		// @}
		//
		// Making flow assignments @{
		$flow = WCFlowManager::I()->currentFlow();
		$flowContainer = $flow ? $flow->item() : false;
		$flowUri = WCFlowManager::I()->currentUrlFlow();
		if($flow && $flow->ok() && $flowContainer && $flowContainer->ok() && $flowContainer->group()->id() == $wcGroup->id()) {
			$flow->setFlowAt($wcImage);
			$flowNav = $flow->getNavigationFlow(WCFlow::SetOfImages);
			if($flowNav) {
				$this->_manager->flow = $flowUri;
				$this->_manager->flow_label = "label_flow_type_{$flow->type()}";
				$this->_manager->flow_name = $flow->item()->name();

				$paramsNoImg = requestParams(true, WC_ACTION_IMAGE_SHOW, array(
					"flow",
					"image"
				));

				$this->_manager->imgfirst = $flowNav[WCFlow::PositionFirst]->preview();
				$this->_manager->imgprev5 = $flowNav[WCFlow::PositionPrevious5]->preview();
				$this->_manager->imgprev = $flowNav[WCFlow::PositionPrevious]->preview();
				$this->_manager->imgcurrent = $flowNav[WCFlow::PositionCurrent]->preview();
				$this->_manager->imgnext = $flowNav[WCFlow::PositionNext]->preview();
				$this->_manager->imgnext5 = $flowNav[WCFlow::PositionNext5]->preview();
				$this->_manager->imglast = $flowNav[WCFlow::PositionLast]->preview();

				$this->_manager->lnkfirst = "{$paramsNoImg}&image={$flowNav[WCFlow::PositionFirst]->id()}&{$flowUri}";
				$this->_manager->lnkprev5 = "{$paramsNoImg}&image={$flowNav[WCFlow::PositionPrevious5]->id()}&{$flowUri}";
				$this->_manager->lnkprev = "{$paramsNoImg}&image={$flowNav[WCFlow::PositionPrevious]->id()}&{$flowUri}";
				$this->_manager->lnkcurrent = "{$paramsNoImg}&image={$flowNav[WCFlow::PositionCurrent]->id()}&{$flowUri}";
				$this->_manager->lnknext = "{$paramsNoImg}&image={$flowNav[WCFlow::PositionNext]->id()}&{$flowUri}";
				$this->_manager->lnknext5 = "{$paramsNoImg}&image={$flowNav[WCFlow::PositionNext5]->id()}&{$flowUri}";
				$this->_manager->lnklast = "{$paramsNoImg}&image={$flowNav[WCFlow::PositionLast]->id()}&{$flowUri}";
			}
		}
		// @}

		$this->_manager->info_readrate = $wcProfile->allowedTo(WC_PERM_IMAGE_SET_RATE, WC_PERM_IMAGE_SET_READ);

		return $ok;
	}
}
